package segger.vasttrafik.parsing;

import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import segger.vasttrafik.TimeTable;
import segger.vasttrafik.Tram;

public class TimeTableParseHandler extends XMLParseHandler {
    private static final String DEPARTURE = "Departure";
    private static final String DEPARTURE_BOARD = "DepartureBoard";
    
    private TimeTable timeTable = null;
    private Tram tram = null;
    
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        if(qName.equalsIgnoreCase(DEPARTURE)) {
            HashMap<String, String> input = new HashMap<String, String>();
            for(int i=0; i<attributes.getLength(); i++) {
                String key = attributes.getQName(i);
                String value = attributes.getValue(i);
                input.put(key, value);
            }
            tram = new Tram(input);
        } else if(qName.equalsIgnoreCase(DEPARTURE_BOARD)) {
            timeTable = new TimeTable();
        }
    }


    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if(qName.equalsIgnoreCase(DEPARTURE)) {
            timeTable.add(tram);
        }
    }

    public TimeTable getTimeTable() {
        return timeTable;
    }

}
